import java.awt.Container;
import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;

import javax.swing.AbstractButton;
import javax.swing.ButtonGroup;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JComponent;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JRadioButton;
import javax.swing.JTextField;

public class addMemberDialog extends JDialog{
	
	// GUI
		private JLabel nameLabel;
		private JTextField nameTextField;
		private JLabel memberIdLabel;
		private JTextField memberIdTextField;
		private JLabel phoneLabel;
		private JTextField phoneTextField;
		private JLabel ageLabel;
		private JTextField ageTextField;
		private JLabel genderLabel;
		private ButtonGroup genderGroup;
		private JRadioButton maleRadiobutton;
		private JRadioButton femaleRadiobutton;
		private JComboBox idComboBox;
		private JTextField idTextField;
		private JLabel timeLabel;
		private Calendar c;
		private String startTime;
		private JLabel startTimeLabel;
		private JButton addButton;
		private JButton cancelButton;
		
		// event handles
		private GenderHandler genderHandler;
		private IdTypeHandler idTypeHandler;
		private AddHandler addHandler;
		private CancelHandler CancelHandler;
		
		// data control
		private memberManager mm;
		private membershipFrame mf;
		private member m;
		
		public addMemberDialog(membershipFrame initMf, memberManager initMm){
			mf = initMf;
			mm = initMm;
			m = new member(-1, "", -1, -1, "", "", "");
			layoutGUI();
		}
		
		private void layoutGUI() {
			// TODO Auto-generated method stub
			this.setTitle("Add Member");
			this.setResizable(false);
			nameLabel = new JLabel("Name: ");
			nameTextField = new JTextField(20);

			memberIdLabel = new JLabel("Member ID: ");
			memberIdTextField = new JTextField(mm.getMemberId());
			memberIdTextField.setEnabled(false);
			
			phoneLabel = new JLabel("Phone: ");
			phoneTextField = new JTextField(20);
			
			ageLabel = new JLabel("Age: ");
			ageTextField = new JTextField(4);
			
			genderLabel = new JLabel("Gender: ");
			genderGroup = new ButtonGroup();
			maleRadiobutton = new JRadioButton("Male ");
			femaleRadiobutton = new JRadioButton("Female ");
			genderGroup.add(maleRadiobutton);
			genderGroup.add(femaleRadiobutton);
			
			idComboBox = new JComboBox();
			String[] idType = {"ID Type","Drive License","Passport"};
			DefaultComboBoxModel idComboBoxModel = new DefaultComboBoxModel(idType);
			idComboBox.setModel(idComboBoxModel);
			idTextField = new JTextField(20);
			idTextField.setEnabled(false);

			timeLabel = new JLabel("From: ");
			DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm");
			c = Calendar.getInstance();
			startTime = dateFormat.format(c.getTime());
			startTimeLabel = new JLabel(startTime);
			
			addButton = new JButton("Add");
			cancelButton = new JButton("Cancel");
			
			// add handlers
			genderHandler = new GenderHandler();
			idTypeHandler = new IdTypeHandler();
			addHandler = new AddHandler();
			CancelHandler = new CancelHandler();
			
			maleRadiobutton.addActionListener(genderHandler);
			femaleRadiobutton.addActionListener(genderHandler);
			idComboBox.addActionListener(idTypeHandler);
			addButton.addActionListener(addHandler);
			cancelButton.addActionListener(CancelHandler);
			
			GridBagLayout layout = new GridBagLayout();
			this.setLayout(layout);
			addComponent(nameLabel,			this,	0,	0,	1,	1);
			addComponent(nameTextField,		this,	1,	0,	4,	1);
			addComponent(memberIdLabel,		this,	0,	1,	1,	1);
			addComponent(memberIdTextField,	this,	1,	1,	4,	1);
			addComponent(phoneLabel,		this,	0,	2,	1,	1);
			addComponent(phoneTextField,	this,	1,	2,	4,	1);
			addComponent(ageLabel,			this,	0,	3,	1,	1);
			addComponent(ageTextField,		this,	1,	3,	1,	1);
			addComponent(genderLabel,		this,	2,	3,	1,	1);
			addComponent(maleRadiobutton,	this,	3,	3,	1,	1);
			addComponent(femaleRadiobutton,	this,	4,	3,	1,	1);
			addComponent(idComboBox,		this,	0,	4,	1,	1);
			addComponent(idTextField,		this,	1,	4,	4,	1);
			addComponent(timeLabel,			this,	0,	5,	1,	1);
			addComponent(startTimeLabel,	this,	1,	5,	2,	1);
			addComponent(addButton,			this,	3,	6,	1,	1);
			addComponent(cancelButton,		this,	4,	6,	1,	1);
			
			// size and locate the dialog at the center of the screen
			this.pack();
			this.setModal(true);
			int w = getWidth();
			int h = getHeight();
			Toolkit tk = Toolkit.getDefaultToolkit();
			Dimension screenSize = tk.getScreenSize();
			int screenWidth = screenSize.width;
			int screenHeight = screenSize.height;
			int x = (screenWidth - w)/2;
			int y = (screenHeight - h)/2;
			setLocation(x,y);
		}
		
		// method helping GridBagLayout
		public void addComponent(	JComponent jc, Container c, 
				int x, int y, int w, int h){
			GridBagConstraints gbc = new GridBagConstraints();
			gbc.gridx = x;
			gbc.gridy = y;
			gbc.gridwidth = w;
			gbc.gridheight = h;
			gbc.fill = GridBagConstraints.HORIZONTAL;
			gbc.insets = new Insets(5, 5, 5, 5);
			c.add(jc, gbc);
		}
		
		class GenderHandler implements ActionListener{

			@Override
			public void actionPerformed(ActionEvent ae) {
				// TODO Auto-generated method stub
				String label = ((AbstractButton) ae.getSource()).getText();
				m.setGender(label);
			}
			
		}
		
		class IdTypeHandler implements ActionListener{

			@Override
			public void actionPerformed(ActionEvent arg0) {
				// TODO Auto-generated method stub
				if (((JComboBox) arg0.getSource()).getSelectedIndex() == 0)
					idTextField.setEnabled(false);
				else idTextField.setEnabled(true);
			}
			
		}
		
		class AddHandler implements ActionListener{

			@Override
			public void actionPerformed(ActionEvent arg0) {
				// TODO Auto-generated method stub
				m.setName(nameTextField.getText());
				m.setMemberID(mm.getMemberId());
				m.setIdType((String) idComboBox.getSelectedItem());
				m.setId(idTextField.getText());
				m.setStartTime(startTime);
				try{
					m.setPhone(Integer.parseInt(phoneTextField.getText()));
					m.setAge(Integer.parseInt(ageTextField.getText()));
				}
				catch (NumberFormatException nfe){
					JOptionPane.showMessageDialog(addMemberDialog.this, "Error: Invalid input", "Error", JOptionPane.ERROR_MESSAGE);
				}
				// update
				mm.update(m);
				if (mm.addMember(m)){
					mf.addMember(m);
					System.out.println("Add Member: \n"+m.toString());
					addMemberDialog.this.setVisible(false);
				}
				else JOptionPane.showMessageDialog(addMemberDialog.this, 
						"Error: Duplicated Members", "Error", JOptionPane.ERROR_MESSAGE);
			}
			
		}
		
		class CancelHandler implements ActionListener{

			@Override
			public void actionPerformed(ActionEvent arg0) {
				// TODO Auto-generated method stub
				addMemberDialog.this.setVisible(false);
			}
			
		}
}
